package com.wardrobe.soa.backend.goods_order;

import com.wardrobe.dao.address.AddressDao;
import com.wardrobe.dao.booking.BookingDao;
import com.wardrobe.dao.goods.GoodsDao;
import com.wardrobe.dao.goods_order.GoodsOrderDao;
import com.wardrobe.dao.shop.ShopDao;
import com.wardrobe.domain.address.AddressBean;
import com.wardrobe.domain.booking.BookingShowBean;
import com.wardrobe.domain.booking_order.BookingOrderShowBean;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.goods.GoodsShowBean;
import com.wardrobe.domain.goods_order.GoodsOrderShowBean;
import com.wardrobe.domain.shop.ShopShowBean;
import com.wardrobe.soa.backend.booking_order.BackBookingOrderFromBean;
import com.wardrobe.soa.backend.booking_order.BackBookingOrderResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/23.
 */
@Service
public class BackGoodsOrderService {
    @Autowired
    private GoodsOrderDao goodsOrderDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private ShopDao shopDao;

    public List<BackGoodsOrderResponse> getGoodsOrderList(BackGoodsOrderFromBean fromBean){
        List<GoodsOrderShowBean> goodsOrderList = goodsOrderDao.selectGoodsOrderList(fromBean);
        List<BackGoodsOrderResponse> list = new ArrayList<>();
        goodsOrderList.stream().forEach(x ->{
            AddressBean addressBean = addressDao.selectDefaultAddress(x.getUserId());
            GoodsShowBean goodsShowBean = goodsDao.selectGoodsById(x.getGoodsId());
            ShopShowBean shopShowBean = shopDao.selectShopById(x.getShopId());
            if(addressBean==null){
                addressBean = new AddressBean();
                addressBean.setName("");
                addressBean.setAddress("");
                addressBean.setMobileNumber("");
            }
            list.add(BackGoodsOrderResponse.builder()
                    .id(x.getId())
                    .userId(x.getUserId())
                    .shopId(x.getShopId())
                    .orderId(x.getOrderId())
                    .payPrice(x.getPayPrice())
                    .remark(x.getRemark())
                    .status(x.getStatus())
                    .goodsName(goodsShowBean.getGoodName())
                    .buyNum(x.getBuyNum())
                    .specifications(x.getSpecifications())
                    .shopName(shopShowBean.getShopName())
                    .userName(addressBean.getName())
                    .address(addressBean.getAddress())
                    .telphone(addressBean.getMobileNumber())
                    .createTime(x.getCreateTime())
                    .payTime(x.getPayTime())
                    .build());
        });
        return list;
    }
    public long countGoodsOrder(BackGoodsOrderFromBean fromBean){
        return goodsOrderDao.countGoodsOrder(fromBean);
    }
    public int modifyGoodsOrder(BackEndFromBean fromBean){
        return goodsOrderDao.updateGoodsOrder(fromBean);
    }
}
